<?php
/**
 * 报表统计
 * @author Administrator
 *
 */
class report extends CControllerMerchantBase{
	
	public function show()
	{
		$this->_title="报表统计";
		
		$icModel = new integralChangeModel();
		
		$s_time = "";
		$e_time = "";
		$owner_card = "";
		$owner_name = "";
		
		$r_type = $this->getRequestData("hidRType","get","");
		
		$is_excel = $this->getRequestData("hidExcel","get","");

		if($r_type)
		{
			if($r_type==1)
			{
				$w = date("w");
				if($w==0)
				{
					$w = 7;
				}
				$s_time = date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-$w+1,date("Y")));
				
				$e_time = date("Y-m-d",mktime(23,59,59,date("m"),date("d")-$w+7,date("Y")));
			}
			else if($r_type==2){
				$s_time = date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
				$e_time = date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
			}
			else if($r_type==3){
				$s_time = date("Y-m-d",mktime(0, 0 , 0,1,1,date("Y")));
				$e_time = date("Y-m-d",mktime(23,59,59,12,31,date("Y")));
			}
		}
		else{
			$s_time=$this->getRequestData("txt_s_time","get",date("Y-m"."-01"));
			$e_time=$this->getRequestData("txt_e_time","get",date("Y-m-d"));
			//$supplier_id=$this->getRequestData("sel_supplier","get","-1");
			$owner_card=$this->getRequestData("txt_card_value","get","");
			$owner_name=$this->getRequestData("txt_name_value","get","");
		}
		//$cost_type=$this->getRequestData("sel_cost_type","get","-1");
		
		$e_time_old = $e_time;
		$e_time = date("Y-m-d",strtotime($e_time." +1 day"));

		$where = "shop_id=".$this->_shopID;
		$where .= " and cost_time>to_date('".$s_time."','yyyy-mm-dd hh24:mi:ss')";
		$where .= " and cost_time<=to_date('".$e_time."','yyyy-mm-dd hh24:mi:ss')";

		$urlParam["txt_s_time"] = $s_time;
		$urlParam["txt_e_time"] = $e_time_old;
		
		if($owner_card)
		{
			$where .= " and owner_card='".$owner_card."'";
			$urlParam["txt_card_value"] = $owner_card;
		}
		if($owner_name)
		{
			$where .= " and owner_name='".$owner_name."'";
			$urlParam["txt_name_value"] = $owner_name;
		}
		
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		
		$searchStr = '时间从'.$s_time.'到'.$e_time_old;
		if($owner_card) 
		{
			$searchStr.= ';卡号：'.$owner_card;
		}
		if($owner_name)
		{
			$searchStr.= ';姓名：'.$owner_name;
		}
		
		$reportData = $icModel->getReport($where);
		
		if($is_excel)
		{
			$list = $icModel->selectDataSetByWhere("t_integral_change c, t_owner o",
						"c_id,s_id,pay_id,cost_money,cost_type,discount_type,discount_num,cost_time,owner_card,cost_shop_before_money,cost_shop_after_money,cost_money_t,owner_name,cancel_cost_type",
						array(),$where." and c.owner_id=o.owner_id order by c_id desc"
					);
			$this->excel($searchStr, $reportData, $list);
		}
		else{
			$pageUrl = $this->getUrlParam($urlParam, array("txt_card_value","txt_name_value"));
			$pageUrl = $pageUrl? "?".$pageUrl:"";
			
			$list = $icModel->selectPage($pageIndex,$pageSize,
				"c_id,s_id,pay_id,cost_money,cost_type,cost_time,discount_type,discount_num,owner_card,cost_shop_before_money,cost_shop_after_money,cost_money_t,owner_name,cancel_cost_type",
			$where." and c.owner_id=o.owner_id","c_id desc","",$c,"t_integral_change c, t_owner o");
			$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);
		}
		
		
		
		/* $supplierModel = new supplierModel(); 
		$sInfo = $supplierModel->selectDataSetByWhere("","s_name,s_id",array(),"del_flag=0 and shop_id=".$this->_shopID);
		
		foreach($list as &$item)
		{
			$payID = $item["pay_id"];
			$payID = substr($payID,0,8)." ".substr($payID,8);
			$item["pay_id"] = $payID;
			
			foreach($sInfo as $sitem)
			{
				if($item["s_id"] == $sitem["s_id"])
				{
					$item["s_name"] = $sitem["s_name"];
				}
			}
		} */

		//$this->setViewParam("sInfo", $sInfo);
		
		$this->setJsFile(array("/My97DatePicker/WdatePicker.js"));
		$this->render("report",array("list"=>$list,"s_time"=>$s_time,
			"e_time"=>$e_time_old,"owner_card"=>$owner_card,"r_type"=>$r_type,
			"owner_name"=>$owner_name,"pageStr"=>$pageStr,"reportData"=>$reportData
		));
	}
	
	private function excel($searchStr, $reportData, $list)
	{
		error_reporting(E_ALL);
		ini_set('display_errors', TRUE);
		ini_set('display_startup_errors', TRUE);
		date_default_timezone_set('Europe/London');
		
		if (PHP_SAPI == 'cli')
			die('This example should only be run from a Web Browser');
		
		/** Include PHPExcel */
		require_once $this->_basePath.'PHPExcel/Classes/PHPExcel.php';
		
		
		// Create new PHPExcel object
		$objPHPExcel = new PHPExcel();
		
		// Set document properties
		$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
		->setLastModifiedBy("Maarten Balliauw")
		->setTitle("Office 2007 XLSX Test Document")
		->setSubject("Office 2007 XLSX Test Document")
		->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
		->setKeywords("office 2007 openxml php")
		->setCategory("Test result file");
		
		$activeSheet = $objPHPExcel->setActiveSheetIndex(0);
		// Add some data
		$activeSheet->setCellValue('A1', '搜索条件')
		->setCellValue('B1', $searchStr)
		->setCellValue('A2', '累计增加积分')
		->setCellValue('C2', '累计扣减积分')
		->setCellValue('A3', '累计撤销增加积分')
		->setCellValue('C3', '累计撤销扣减积分')
		->setCellValue('A4', '累计实际增加积分')
		->setCellValue('C4', '累计实际扣减积分')
		->setCellValue('B2', $reportData["cost_money_add"])
		->setCellValue('D2', $reportData["cost_money_reduce"])
		->setCellValue('B3', $reportData["cost_money_add_cancel"])
		->setCellValue('D3', $reportData["cost_money_reduce_cancel"])
		->setCellValue('B4', $reportData["cost_money_add_sj"])
		->setCellValue('D4', $reportData["cost_money_reduce_sj"])
		->setCellValue('A6', '交易流水号')
		->setCellValue('B6', '交易时间')
		->setCellValue('C6', '交易类别')
		->setCellValue('D6', '卡号')
		->setCellValue('E6', '业主姓名')
		->setCellValue('F6', '实际增加积分')
		->setCellValue('G6', '赠送积分')
		->setCellValue('H6', '扣减积分')
		->setCellValue('I6', '当前剩余积分');
		
		$i = 7;
		foreach($list as $item)
		{
			$cost_type="";
			if($item["cost_type"]==1)
			{
				$cost_type = "增加积分";
			}
			else if($item["cost_type"]==2)
			{
				$cost_type = "扣减积分";
			}
			else if($item["cost_type"]==3)
			{
				$cost_type = "撤销交易";
			}
			
			$add = 0;
			$reduce = 0;
			$zengsong = 0;
			if($item["cost_type"]==1 || ($item["cost_type"]==3 && $item["cancel_cost_type"]==2))
			{
				$add = $item["cost_money_t"];
			}
			
			if($item["cost_type"]==1 && $item["discount_type"]==1)
			{
				$zengsong = $item["discount_num"];
			}
			
			if($item["cost_type"]==2 || ($item["cost_type"]==3 && $item["cancel_cost_type"]==1))
			{
				$reduce = $item["cost_money_t"];
			}
			
			$activeSheet->setCellValue("A".$i,$item["pay_id"])
			->setCellValue("B".$i,$item["cost_time"])
			->setCellValue("C".$i,$cost_type)
			->setCellValue("D".$i,$item["owner_card"])
			->setCellValue("E".$i,$item["owner_name"])
			->setCellValue("F".$i,$add)
			->setCellValue("G".$i,$zengsong)
			->setCellValue("H".$i,$reduce)
			->setCellValue("I".$i,$item["cost_shop_after_money"]);
			$i++;
		}
		
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle('报表');
		
		
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);
		
		
		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="报表.xls"');
		header('Cache-Control: max-age=0');
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;
		
	}
}